<template>
  <div>
    <Header></Header>
    <section>
      <van-address-list
        v-if="list.length"
        :list="list"
        add-button-text="添加地址"
        default-tag-text="默认"
        @add="goList('add')"
        @edit="goList"
      />
      <h1 v-else>暂无数据，请添加地址
        <van-address-list
        @add="goList('add')"
      /></h1>
      
    </section>
    <Tabbar></Tabbar>
  </div>
</template>

<script>
import http from "@/common/api/request.js";
import Header from "@/components/path/Header.vue";
import Tabbar from "@/components/common/Tabbar.vue";
import { mapState, mapMutations } from "vuex";
import bus from "@/common/bus.js";
export default {
  data() {
    return {
      pathStatus: false,
    };
  },
  components: {
    Header,
    Tabbar,
  },
  created() {
    //从订单页面进来的
    if (this.$route.query.type == "select") {
      this.pathStatus = true;
    }

    this.getData();
  },
  computed: {
    ...mapState({
      list: (state) => state.path.list,
    }),

  },
  methods: {
    ...mapMutations(["initData"]),
    getData() {
      http
        .$axios({
          url: "/api/selectAddress",
          method: "post",
          headers: {
            // 获取当前的token值
            token: window.localStorage.getItem("teaUserInfo"),
          },
        })
        .then((res) => {
          this.initData(res.data);
          console.log('res.data', res.data)
        });
    },
    goList(option) {
      //this.pathStatus为true代表从订单页面进入的：选择状态
      if (this.pathStatus) {
        bus.$emit("selectPath", JSON.stringify(option));
        this.$router.back();
        return;
      }

      this.$router.push({
        name: "path-list",
        params: {
          key: JSON.stringify(option),
        },
      });
    },
  },
};
</script>

<style scoped>
.van-address-list .van-address-list__bottom {
  position: relative;
}

.van-button--danger {
  background-color: rgb(176, 53, 47);
}

.van-tag--danger {
  background-color: rgb(176, 53, 47);
}

.add-path {}
</style>
